home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Tools & Goodies / IntlTest / Sources / Dialogs.cpp < prev    next >
Encoding:
Text File  |  1996-09-12  |  15.4 KB  |  502 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Dialogs.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "IntlTest.hpp"
  13.  
  14. #ifndef DIALOGS_H
  15. #include "Dialogs.h"
  16. #endif
  17.  
  18. #ifndef PART_H
  19. #include "Part.h"
  20. #endif
  21.  
  22. #ifndef DEFINES_K
  23. #include "Defines.k"
  24. #endif
  25.  
  26. #ifndef EDITVIEWS_H
  27. #include "EditViews.h"
  28. #endif
  29.  
  30. // ----- ODF Includes -----
  31.  
  32. #ifndef FWEDVIEW_H
  33. #include "FWEdView.h"
  34. #endif
  35.  
  36. #ifndef FWSTATIC_H
  37. #include "FWStatic.h"
  38. #endif
  39.  
  40. #ifndef FWCONTXT_H
  41. #include "FWContxt.h"
  42. #endif
  43.  
  44. #ifndef FWBUTTON_H
  45. #include "FWButton.h"
  46. #endif
  47.  
  48. #ifndef FWKEYF_H
  49. #include "FWKeyF.h"
  50. #endif
  51.  
  52. #ifndef FWNOTDEF_H
  53. #include "FWNotDef.h"
  54. #endif
  55.  
  56. #ifndef FWCFMRES_H
  57. #include "FWCFMRes.h"
  58. #endif
  59.  
  60. #ifndef FWTXTSHP_H
  61. #include "FWTxtShp.h"
  62. #endif
  63.  
  64. #ifndef FWTXTBOX_H
  65. #include "FWTxtBox.h"
  66. #endif
  67.  
  68. //========================================================================================
  69. // Runtime Information
  70. //========================================================================================
  71.  
  72. #ifdef FW_BUILD_MAC
  73. #pragma segment odfform
  74. #endif
  75.  
  76. void ClearResultView(Environment* ev, FW_CStaticText* resultView, FW_CDialogFrame* dialogFrame);
  77.  
  78. //----------------------------------------------------------------------------------------
  79. void ClearResultView(Environment* ev, FW_CStaticText* resultView, FW_CDialogFrame* dialogFrame)
  80. {
  81.     // Draw the existing text in erase mode
  82.     FW_CTextBoxShape* tbShape = resultView->GetTextBoxShape(ev);
  83.     FW_CInk ink = tbShape->GetInk();
  84.     FW_TransferModes savedMode = ink.GetTransferMode();
  85.     ink.SetTransferMode(FW_kErase);
  86.     resultView->Draw(ev, dialogFrame->GetActiveFacet(ev), NULL);
  87.     ink.SetTransferMode(savedMode);    // reset to original transfer mode
  88.     tbShape->SetInk(ink);
  89. }
  90.  
  91. //========================================================================================
  92. // COpTestDialogFrame class
  93. //========================================================================================
  94.  
  95. FW_DEFINE_AUTO(COpTestDialogFrame)
  96.  
  97. //----------------------------------------------------------------------------------------
  98. COpTestDialogFrame::COpTestDialogFrame(Environment* ev, 
  99.                 ODFrame* odFrame, 
  100.                 FW_CPresentation* presentation, 
  101.                 CIntlTestPart* part)
  102. :    FW_CDialogFrame(ev, odFrame, presentation, part),
  103.     fTestPart(part),
  104.     fIsJapanese(false),
  105.     fResultView(NULL)
  106. {
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. COpTestDialogFrame::~COpTestDialogFrame()
  111. {
  112. }
  113.  
  114. //----------------------------------------------------------------------------------------
  115. void COpTestDialogFrame::Initialize(Environment* ev, CMyEditView* editView)
  116. {
  117.     fTargetString = editView->GetText(ev);    // sets string's locale
  118.     FW_Locale locale;
  119.     editView->GetLocale(ev, locale);
  120.     fIsJapanese = (locale.fScriptCode == smJapanese);
  121.  
  122.     FW_CView* view = FindViewByID(ev, kStaticResultID);
  123.     fResultView = FW_DYNAMIC_CAST(FW_CStaticText, view);
  124.     FW_ASSERT(fResultView);
  125.  
  126.     // Change the result view's font to that of the edit view
  127.     FW_CFont font = editView->GetCurrentFont();
  128.     FW_CTextBoxShape* textBoxShape = fResultView->GetTextBoxShape(ev);
  129.     textBoxShape->SetFont(font);
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. void COpTestDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  134. {
  135.     // ---- Verify edit fields when clicking on OK button ----
  136.     if (notification.GetMessage() ==  FW_kDefaultButtonMsg) 
  137.     {
  138.         // Get the edit view prompting for the index
  139.         FW_CView* view =  FindViewByID(ev, kIndexEditID);
  140.         FW_CEditView* indexEditView = FW_DYNAMIC_CAST(FW_CEditView, view);
  141.         FW_ASSERT(indexEditView);
  142.  
  143.         // Get the index the user typed in
  144.         FW_CString indexString = indexEditView->GetText(ev);
  145.  
  146.         // String must have at least 1 character
  147.         if (indexString.GetCharacterLength() > 0)
  148.         {
  149.             FW_PSharedLibraryResourceFile resFile(ev);
  150.             FW_CString msg;
  151.  
  152.             // Convert string to a number
  153.             long index = indexString.ParseAsUnsignedInteger();
  154.  
  155.             // Check that the index is not larger than the target string
  156.             if (index >= fTargetString.GetCharacterLength())    // 0-based index
  157.             {
  158.                 // Display error message and return to avoid closing the dialog
  159.                 if (fIsJapanese)
  160.                     // load Japanese string: "Bango ha ookisugiru"
  161.                     msg = MyLoadRStringByID(ev, resFile, kJTooBigStringID, TESTSTRINGRES);
  162.                 else
  163.                     // load English string: "Number is too large"
  164.                     FW_LoadStringByID(ev, resFile, kIntlTestStrings, FW_kMultiStringRes, kEngTooBigStringID, msg);
  165.                 FW_Beep();
  166.                 fResultView->SetText(ev, msg, FW_kInvalidate);
  167.                 return;
  168.             }
  169.  
  170.             // Retrieve the index'th character and display it
  171.             FW_CString substitutionString;
  172.             if (fIsJapanese)
  173.             {    // Compose msg string: "nbanme no ji ha ‘**’ desu"
  174.                 FW_LChar chj = fTargetString[index];
  175.                 msg = MyLoadRStringByID(ev, resFile, kJNthCharStringID, TESTSTRINGRES);
  176.                 msg.Insert(indexString, 0);
  177.                 substitutionString.Append((const char*)&chj, 2);
  178.                 msg.Substitute("**", substitutionString);
  179.             }
  180.             else
  181.             {    // Compose msg string: "Character #^0 is ‘^1’"
  182.                 FW_Char ch = fTargetString[index];
  183.                 FW_LoadStringByID(ev, resFile, kIntlTestStrings, FW_kMultiStringRes, kEngNthCharStringID, msg);
  184.                 substitutionString.Append(ch);
  185.                 msg.Substitute("^0", indexString);
  186.                 msg.Substitute("^1", substitutionString);
  187.             }
  188.  
  189.             // Display msg string 
  190.             fResultView->SetText(ev, msg, FW_kInvalidate);
  191.         }
  192.         return;    // don't call inherited, because that will close the dialog
  193.     }
  194.     
  195.     // Must call inherited to close the dialog on OK & Cancel
  196.     // WARNING: do not do anything else after this call... the dialog is gone!
  197.     FW_CDialogFrame::HandleNotification(ev, notification);
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. void COpTestDialogFrame::PostCreateViewFromStream(Environment* ev)
  202. {
  203.     // ----- Call inherited to propagate to all subviews if necessary
  204.     FW_CSuperView::PostCreateViewFromStream(ev);
  205.  
  206.     // ----- Add numeric keyfilter to the index prompt view
  207.     //        (will be deleted along with the edit view, it's an adopted event handler)
  208.     FW_CView* editView1 = FindViewByID(ev, kIndexEditID);    
  209.     FW_CIntegerFilter* keyF1 = new FW_CIntegerFilter(ev, (FW_MEventHandler*)editView1);
  210.     
  211.     // Note: OK and Cancel buttons are linked automatically to the dialogFrame by
  212.     //         defining FW_kDefaultButtonMsg and FW_kCancelButtonMsg in "Views.fr"
  213.  
  214.     // WARNING: Make sure that classes created from resources won't be dead-stripped
  215.     FW_DO_NOT_DEAD_STRIP(FW_CButton);
  216.     FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
  217.     FW_DO_NOT_DEAD_STRIP(FW_CEditView);
  218. }
  219.  
  220. //========================================================================================
  221. // CReaderTestDialogFrame class
  222. //========================================================================================
  223.  
  224. FW_DEFINE_AUTO(CReaderTestDialogFrame)
  225.  
  226. //----------------------------------------------------------------------------------------
  227. CReaderTestDialogFrame::CReaderTestDialogFrame(Environment* ev, 
  228.                             ODFrame* odFrame, 
  229.                             FW_CPresentation* presentation, 
  230.                             CIntlTestPart* part)
  231. :    FW_CDialogFrame(ev, odFrame, presentation, part),
  232.     fTestPart(part),
  233.     fIsJapanese(false),
  234.     fCharacterView(NULL),
  235.     fPositionView(NULL),
  236.     fReader(NULL),
  237.     fBytePosition(0)
  238. {
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. CReaderTestDialogFrame::~CReaderTestDialogFrame()
  243. {
  244.     if (fReader)
  245.         delete fReader;
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. void CReaderTestDialogFrame::Initialize(Environment* ev, CMyEditView* editView)
  250. {
  251.     fTargetString = editView->GetText(ev);    // sets string's locale
  252.     FW_Locale locale;
  253.     editView->GetLocale(ev, locale);
  254.     fIsJapanese = (locale.fScriptCode == smJapanese);
  255.  
  256.     fReader = FW_NEW(FW_CStringReader, (fTargetString));
  257.  
  258.     FW_CView* view = FindViewByID(ev, kStaticResultID);
  259.     fCharacterView = FW_DYNAMIC_CAST(FW_CStaticText, view);
  260.     FW_ASSERT(fCharacterView);
  261.  
  262.     // Change the result view's font to that of the edit view
  263.     FW_CFont font = editView->GetCurrentFont();
  264.     FW_CTextBoxShape* textBoxShape = fCharacterView->GetTextBoxShape(ev);
  265.     textBoxShape->SetFont(font);
  266.  
  267.     view = FindViewByID(ev, kBytePositionID);
  268.     fPositionView = FW_DYNAMIC_CAST(FW_CStaticText, view);
  269.     FW_ASSERT(fPositionView);
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. void CReaderTestDialogFrame::PostCreateViewFromStream(Environment* ev)
  274. {
  275.     // ----- Call inherited to propagate to all subviews if necessary
  276.     FW_CSuperView::PostCreateViewFromStream(ev);
  277.  
  278.     // ----- Set up connections for non-default buttons
  279.     FW_CControl* button = (FW_CControl*) FindViewByID(ev, kGetCharAdvButtonID);
  280.     button->LinkControlTo(ev, this);
  281.     button = (FW_CControl*) FindViewByID(ev, kBackupGetCharButtonID);
  282.     button->LinkControlTo(ev, this);
  283.     button = (FW_CControl*) FindViewByID(ev, kAdvanceButtonID);
  284.     button->LinkControlTo(ev, this);
  285.     button = (FW_CControl*) FindViewByID(ev, kBackupButtonID);
  286.     button->LinkControlTo(ev, this);
  287.     button = (FW_CControl*) FindViewByID(ev, kPeekCharButtonID);
  288.     button->LinkControlTo(ev, this);
  289. }
  290.  
  291. //----------------------------------------------------------------------------------------
  292. void CReaderTestDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  293. {
  294.     // ---- Verify edit fields when clicking on OK button ----
  295.     if (notification.GetMessage() ==  FW_kButtonPressedMsg)
  296.     {
  297.         const FW_CControlNotification& controlNot = (FW_CControlNotification&) notification;
  298.         ODID buttonID = controlNot.GetViewID(ev);
  299.  
  300.         switch (buttonID)
  301.         {
  302.             case kGetCharAdvButtonID:
  303.                 GetCharacterAndAdvanceReader(ev);
  304.                 break;
  305.  
  306.             case kBackupGetCharButtonID:
  307.                 BackupReaderAndGetCharacter(ev);
  308.                 break;
  309.  
  310.             case kAdvanceButtonID:
  311.                 AdvanceReader(ev);
  312.                 break;
  313.  
  314.             case kBackupButtonID:
  315.                 BackupReader(ev);
  316.                 break;
  317.  
  318.             case kPeekCharButtonID:
  319.                 PeekCharReader(ev);
  320.                 break;
  321.         }
  322.         return;    // don't close dialog box
  323.     }
  324.     
  325.     // Must call inherited to close the dialog on OK and Cancel
  326.     // WARNING: do not do anything else after this call... the dialog is gone!
  327.     FW_CDialogFrame::HandleNotification(ev, notification);
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. void CReaderTestDialogFrame::GetCharacterAndAdvanceReader(Environment* ev)
  332. {
  333.     FW_LChar ch = fReader->GetCharacterAndAdvance();
  334.     fBytePosition = fReader->GetPosition();
  335.     UpdateDisplay(ev, ch);
  336. }
  337.  
  338. void CReaderTestDialogFrame::BackupReaderAndGetCharacter(Environment* ev)
  339. {
  340.     FW_LChar ch = fReader->BackupAndGetCharacter();
  341.     fBytePosition = fReader->GetPosition();
  342.     UpdateDisplay(ev, ch);
  343. }
  344.  
  345. void CReaderTestDialogFrame::AdvanceReader(Environment* ev)
  346. {
  347.     fReader->Advance();
  348.     fBytePosition = fReader->GetPosition();
  349.     PeekCharReader(ev);
  350. }
  351.  
  352. void CReaderTestDialogFrame::BackupReader(Environment* ev)
  353. {
  354.     fReader->Backup();
  355.     fBytePosition = fReader->GetPosition();
  356.     PeekCharReader(ev);
  357. }
  358.  
  359. void CReaderTestDialogFrame::PeekCharReader(Environment* ev)
  360. {
  361.     FW_LChar ch = fReader->PeekCharacter();
  362.     UpdateDisplay(ev, ch);
  363. }
  364.  
  365. //----------------------------------------------------------------------------------------
  366. void CReaderTestDialogFrame::UpdateDisplay(Environment* ev, FW_LChar ch)
  367. {
  368.     FW_CString string;
  369.     if (fIsJapanese && (ch & 0xFF00))
  370.     {
  371.         // double-byte character
  372.         unsigned short bytes = ch;
  373.         string.Append((char*)&bytes, 2);
  374.     }
  375.     else
  376.         string.Append(ch);    // single-byte character
  377.  
  378.     fCharacterView->SetText(ev, string, FW_kInvalidate);
  379.  
  380.     string.ReplaceAllAsUnsignedDecimalInteger(fBytePosition);
  381.     fPositionView->SetText(ev, string, FW_kInvalidate);
  382. }
  383.  
  384. //==============================================================================
  385. // class CNumberTestDialogFrame
  386. //==============================================================================
  387.  
  388. FW_DEFINE_AUTO(CNumberTestDialogFrame)
  389.  
  390. //----------------------------------------------------------------------------------------
  391. CNumberTestDialogFrame::CNumberTestDialogFrame(Environment* ev, 
  392.                 ODFrame* odFrame, 
  393.                 FW_CPresentation* presentation, 
  394.                 CIntlTestPart* part)
  395. :    FW_CDialogFrame(ev, odFrame, presentation, part),
  396.     fTestPart(part),
  397.     fIsNewTest(false),
  398.     fIsJapanese(false),
  399.     fResultView(NULL)
  400. {
  401. }
  402.  
  403. //----------------------------------------------------------------------------------------
  404. CNumberTestDialogFrame::~CNumberTestDialogFrame()
  405. {
  406. }
  407.  
  408. //----------------------------------------------------------------------------------------
  409. void CNumberTestDialogFrame::Initialize(Environment* ev, FW_Boolean testNewCode)
  410. {
  411.     fIsNewTest = testNewCode;
  412.  
  413.     FW_CView* view = FindViewByID(ev, kStaticResultID);
  414.     fResultView = FW_DYNAMIC_CAST(FW_CStaticText, view);
  415.     FW_ASSERT(fResultView);
  416. }
  417.  
  418. //----------------------------------------------------------------------------------------
  419. void CNumberTestDialogFrame::PostCreateViewFromStream(Environment* ev)
  420. {
  421.     // ----- Call inherited to propagate to all subviews if necessary
  422.     FW_CSuperView::PostCreateViewFromStream(ev);
  423.  
  424.     // ----- Set up connections for non-default buttons
  425.     FW_CControl* button = (FW_CControl*) FindViewByID(ev, kJCheckBoxID);
  426.     button->LinkControlTo(ev, this);
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. void CNumberTestDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  431. {
  432.     // ---- Verify edit fields when clicking on OK button ----
  433.     if (notification.GetMessage() ==  FW_kDefaultButtonMsg) 
  434.     {
  435.         // Get the edit view prompting for the index
  436.         FW_CView* view =  FindViewByID(ev, kNumberInputID);
  437.         CMyEditView* inputView = FW_DYNAMIC_CAST(CMyEditView, view);
  438.         FW_ASSERT(inputView);
  439.  
  440.         // Get the number the user typed in
  441.         FW_CString userString = inputView->GetText(ev);
  442.  
  443.         // Input string must have at least 1 character
  444.         if (userString.GetCharacterLength() > 0)
  445.         {
  446.             FW_PSharedLibraryResourceFile resFile(ev);
  447.             double result;
  448.  
  449.             // Convert string to a number
  450.             result = userString.ParseAsRealNumber();
  451.  
  452.             // Display result 
  453.             FW_CString msg;
  454.             msg.ReplaceAllAsRealNumber(result, 8);
  455.             msg.Prepend("Result: ‘");
  456.             msg.Append("’");
  457.             fResultView->SetText(ev, msg, FW_kInvalidate);
  458.         }
  459.         else
  460.             ClearResultView(ev, fResultView, this);
  461.  
  462.         return;    // don't call inherited, because that will close the dialog
  463.     }
  464.     else if (notification.GetMessage() == FW_kButtonPressedMsg)
  465.     {
  466.         const FW_CControlNotification& controlNotification = (FW_CControlNotification&) notification;
  467.         ODID viewId = controlNotification.GetViewID(ev);
  468.         if (viewId == kJCheckBoxID)
  469.         {
  470.             this->SetInputFont(ev, (controlNotification.GetControl(ev)->GetValue(ev) == 1));
  471.         }
  472.     }
  473.     
  474.     // Must call inherited to close the dialog on Cancel
  475.     // WARNING: do not do anything else after this call... the dialog may be gone!
  476.     FW_CDialogFrame::HandleNotification(ev, notification);
  477. }
  478.  
  479. //----------------------------------------------------------------------------------------
  480. void CNumberTestDialogFrame::SetInputFont(Environment* ev, FW_Boolean setJapanese)
  481. {
  482.     // First get a reference to the edit view
  483.     FW_CView* view =  FindViewByID(ev, kNumberInputID);
  484.     FW_CEditView* inputView = FW_DYNAMIC_CAST(FW_CEditView, view);
  485.     FW_ASSERT(inputView);
  486.  
  487.     // Set the font
  488.     fIsJapanese = setJapanese;
  489.     if (setJapanese)
  490.     {
  491.         FW_CString jString("", gJapaneseLocale);
  492.         inputView->SetFont(ev, gFontOsaka);
  493.         inputView->SetText(ev, jString);
  494.     }
  495.     else
  496.     {
  497.         inputView->SetFont(ev, FW_CFont(FW_GetHelveticaFontName(), FW_kPlain, FW_IntToFixed(12)));
  498.         inputView->ClearText(ev);
  499.     }
  500. }
  501.  
  502.